package com.framework.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;
import static org.openqa.selenium.support.ui.ExpectedConditions.textToBePresentInElement;

public class AddMoviePage extends AnyPage {

    public AddMoviePage(PageManager pages) {super(pages);}

    @FindBy (name = "name")
    private WebElement title;

    @FindBy (name = "year")
    private WebElement year;

    @FindBy (name = "submit")
    private WebElement submit;

    @FindBy (xpath = "//*[@id=\"updateform\"]/table/tbody/tr[2]/td[2]/label")
    private WebElement noTitleError;

    @FindBy (xpath = "//*[@id=\"updateform\"]/table/tbody/tr[4]/td[2]/label")
    private WebElement noYearError;

    public AddMoviePage ensurePageLoaded() {
        wait.until(presenceOfElementLocated(By.id("imdbsearch")));
        return this;
    }

    public AddMoviePage setTitle(String text) {
        title.sendKeys(text);
        return this;
    }

    public AddMoviePage setYear(String text) {
        year.sendKeys(text);
        return this;
    }

    public void clickSubmitButton() {
        submit.click();
    }

    public boolean checkNoTitleError() {
        return wait.until(textToBePresentInElement(noTitleError, "This field is required"));
    }

    public boolean checkNoYearError() {
        return wait.until(textToBePresentInElement(noYearError, "This field is required"));
    }

}
